﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Text.RegularExpressions;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace Syrinx
{
	public class SecurePagesBizRules : BaseBizRules
	{
		public IBizObjList getSecurePages(IApplication app, IRuleContext context)
		{
			FlexiMap m = new FlexiMap();
			m["companyId"] = ActiveCompanyId;
			IBizObjList pages = app.appData.get("DA.Syrinx.GetCompanySecurePages", m) as IBizObjList;
			return pages;
		}

		public void updateSecurePage(IApplication app, IRuleContext context, IBizObj page)
		{
			int c = 0;
			if (page.isNew)
				c = (int)app.appData.get("DA.Syrinx.AddCompanySecurePage", page);
			else
				c = (int)app.appData.get("DA.Syrinx.UpdateCompanySecurePage", page);
		}

		public void deleteSecurePage(IApplication app, IRuleContext context, Swaf.BGuid.Guid pageId)
		{
			FlexiMap m = new FlexiMap();
			m["id"] = pageId;
			int c = (int)app.appData.get("DA.Syrinx.DeleteCompanySecurePage", m);
		}

		public bool isSecurePage(IApplication app, IRuleContext context, string pageName)
		{
			bool isSecure = false;

			if (Cmn.IsTrue(ActiveCompany.get("settings.useSslOnAllPages")))
				isSecure = true;
			else
			{
				FlexiMap m = new FlexiMap();
				m["pageName"] = pageName;
				m["companyId"] = ActiveCompanyId;
				IBizObj page = app.appData.get("DA.Syrinx.GetSecurePageFromPageName", m) as IBizObj;
				isSecure = page != null;
			}

			return isSecure;
		}

		public string getSecurePagesCompanySettingsTemplate(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			return resultValue.ToString() + ",Syrinx.Gui.AspNet.SecurePagesCompanySettingsDisplay";
		}

		public IBizObjList getSecurePagesCompanySettingsDbFields(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObjList settingsFields = resultValue as IBizObjList;

			IBizObj eca = settingsFields.insertNew();
			eca.put("dbName", "useSslOnAllPages");
			eca.put("bizObjName", "useSslOnAllPages");
			eca.put("aliasName", "useSslOnAllPages");
			eca.put("dbType", "Boolean");

			return settingsFields;
		}
	}
}
